//
//  COBalanceViewCell.swift
//  CocoProject
//
//  Created by Vill on 2023/11/14.
//

import UIKit

class COBalanceViewCell: UITableViewCell {
    
    
    @IBOutlet weak var containBg: UIView!
    
    @IBOutlet weak var priceLbl: UILabel!
    
    @IBOutlet weak var numLbl: UILabel!
    
    @IBOutlet weak var newUserLbl: UILabel!
    
    static let reusedID = "COBalanceViewCell"
    public func cellWithTableView(_ tableView:UITableView) -> COBalanceViewCell{
        var cell:COBalanceViewCell! = tableView.dequeueReusableCell(withIdentifier: COBalanceViewCell.reusedID) as? COBalanceViewCell
        if cell == nil {
            cell = (Bundle.main.loadNibNamed("COBalanceViewCell", owner: nil, options: nil)?.first as! COBalanceViewCell)
        }
        return cell!
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        self.containBg.layer.cornerRadius = PTW(20)
        self.containBg.layer.masksToBounds = true
        
        self.priceLbl.layer.cornerRadius = 13
        self.priceLbl.layer.masksToBounds = true
        
        self.newUserLbl.layer.cornerRadius = 15
        self.newUserLbl.layer.masksToBounds = true
        
    }
    
    var model:COBalanceModel?{
        didSet {
            guard let model = model else {
                return
            }
            priceLbl.text = String(format: "$%.2f", model.price)
            if(model.free > 0){
                let realStr = "\(model.real)"
                let freeStr = "+\(model.free)"
                let arr = NSMutableAttributedString(string: realStr+freeStr)
                arr.addAttributes([.foregroundColor:UIColor.colorWithHex("#8835FF")], range: NSRange(location: realStr.count, length: freeStr.count))
                self.numLbl.attributedText = arr
            }else{
                self.numLbl.text = "\(model.coins)"
            }
            // 赠送金币大于0
            if (model.activity == 1){
                self.newUserLbl.isHidden = false
                self.newUserLbl.text = "New User"
                self.newUserLbl.backgroundColor = UIColor.gradientFromColor(colors: [UIColor.colorWithHex("#FF9B32").cgColor,UIColor.colorWithHex("#FF3588").cgColor], direction: .Horizontal, range: 77)
            }else{
                if(model.free > 0){
                    self.newUserLbl.isHidden = false
                    self.newUserLbl.text =  String(format: "%.f%% off",( 1 - model.price/model.realPrice)*100)
                    self.newUserLbl.backgroundColor =  UIColor.gradientFromColor(colors: [UIColor.colorWithHex("#7038FF").cgColor,UIColor.colorWithHex("#FF3DE1").cgColor], direction: .Horizontal, range: 77)
                }else{
                    self.newUserLbl.isHidden = true
                }
            }
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
}
